/*
* ScoreCounter
*
* Copyright (C) Connect Link 2013-
*/


package com.connectlinkcorp.common.ui.scorecounter;

import java.util.ArrayList;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ScoreCounter extends LinearLayout{
	
	
	private Context context;
	protected int duration;
	protected int completeSound;
	protected int countingUpSound;
	protected int fromScore;
	protected int toScore;
	protected int positionX,positionY,width,height;
	private ArrayList<ImageView> arrImages;
	private int imageScore[];
	private boolean isRun;
	/*
	 * 
	 */
	public ScoreCounter(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		isRun = false;
	}
	/*
	 * 
	 */
	public ScoreCounter(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		isRun = false;
	}
	/*
	 * 
	 */
	public ScoreCounter(Context context,int duration,int completeSound,int countingUpSound,
			int fromScore,int toScore,int positionX,int positionY,
			int width,int height) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.duration = duration;
		this.completeSound = completeSound;
		this.countingUpSound = countingUpSound;
		this.fromScore = fromScore;
		this.toScore = toScore;
		this.positionX = positionX;
		this.positionY = positionY;
		this.width = width;
		this.height = height;
		isRun = false;
	}
	/*
	 * setUpParam
	 */
	public void setUpParam(Context context,int duration,int completeSound,int countingUpSound,
			int positionX,int positionY,int width,int height){
		// TODO Auto-generated constructor stub
		this.context = context;
		this.duration = duration;
		this.completeSound = completeSound;
		this.countingUpSound = countingUpSound;
		this.positionX = positionX;
		this.positionY = positionY;
		this.width = width;
		this.height = height;
	}
	
	/*
	 * show
	 */
	public void show(){
		LayoutParams params = new LayoutParams(width,height);
		params.setMargins(positionX, positionY, 0,0);
		this.setLayoutParams(params);
	}
	
	/*
	 * startTask
	 */
	private void startTask(Runnable runnable){
		isRun = true;
		Thread thread = new Thread(runnable);
		thread.start();
	}
	/*
	 * start
	 */
	public void start(final int fromScore,final int toScore){
		if (!isRun) {
			this.fromScore = fromScore;
			this.toScore = toScore;
			arrImages = new ArrayList<ImageView>();
			this.removeAllViews();
			for(int i = 0;i<toScore;i++){
				ImageView temp = new ImageView(context);
				arrImages.add(temp);
				this.addView(temp);
			}
			startTask(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						for(int i = fromScore;i<=toScore;i++){
							handlerDisplay.sendEmptyMessage(i);
							Thread.sleep(duration);
						}
					} catch (Exception ex) {
						// TODO: handle exception
						Log.d("Error", ex.getMessage());
					}
					
				}
			});
		}
		
	}
	/*
	 * handlerDisplay
	 */
	Handler handlerDisplay = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			int index = msg.what;
			String strIndex = String.valueOf(index);
			for(int i = 0;i<strIndex.length();i++){
				index = Integer.parseInt(String.valueOf(strIndex.charAt(i)));
				arrImages.get(i).setBackgroundResource(imageScore[index]);
			}
		}
		
	};
	
	public int getDuration() {
		return duration;
	}
	public void setDuration(int duration) {
		this.duration = duration;
	}
	public int getCompleteSound() {
		return completeSound;
	}
	public void setCompleteSound(int completeSound) {
		this.completeSound = completeSound;
	}
	public int getCountingUpSound() {
		return countingUpSound;
	}
	public void setCountingUpSound(int countingUpSound) {
		this.countingUpSound = countingUpSound;
	}
	public int getFromScore() {
		return fromScore;
	}
	public void setFromScore(int fromScore) {
		this.fromScore = fromScore;
	}
	public int getToScore() {
		return toScore;
	}
	public void setToScore(int toScore) {
		this.toScore = toScore;
	}
	public int getPositionX() {
		return positionX;
	}
	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}
	public int getPositionY() {
		return positionY;
	}
	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}
	public int getWidthOfView() {
		return width;
	}
	public void setWidthOfView(int width) {
		this.width = width;
	}
	public int getHeightOfView() {
		return height;
	}
	public void setHeightOfView(int height) {
		this.height = height;
	}
	public ArrayList<ImageView> getArrImages() {
		return arrImages;
	}
	public void setArrImages(ArrayList<ImageView> arrImages) {
		this.arrImages = arrImages;
	}
	public int[] getImageScore() {
		return imageScore;
	}
	public void setImageScore(int[] imageScore) {
		this.imageScore = imageScore;
	}
}




















